﻿
@using Blazor.Diagrams.Core.Extensions
 
@using Blazor.Diagrams.Core.Models
@using ERP.Workflow.Designer.BlazorUI.Models
@using Blazor.Diagrams.Core.Geometry
 
  @*  @((MarkupString)*@

@if (!string.IsNullOrWhiteSpace(Label.Content))
{
    <foreignObject x="@X.ToInvariantString()" y="@Y.ToInvariantString()">
        <div class="link-label" @ondblclick="DbClick">  
            @if (IsEdit)
            {
                <input @ref="InputRef"  @bind-value="@Label.Content" @onblur="CancleEdit" />
            }
            else
            {
              <span @ondblclick="DbClick">@Label.Content</span>
            }
        
        </div>
    </foreignObject>
}


@code {
    [Parameter] public LinkLabelModel? Label { get; set; }
    [Parameter] public Point? Position { get; set; }

    private double X => Position.X + (Label.Offset?.X ?? 0);
    private double Y => Position.Y + (Label.Offset?.Y ?? 0);
    protected bool IsEdit = false;
    protected ElementReference InputRef;


    protected override Task OnInitializedAsync()
    {
        Label.Changed += StateHasChanged;
        return base.OnInitializedAsync();
    }
    protected void DbClick()
    {
        Edit();
        StateHasChanged();
        InputRef.FocusAsync();
    }

    public void Edit()
    {
        IsEdit = true;
        StateHasChanged();
    }
    public void CancleEdit()
    {
        IsEdit = false;
        StateHasChanged();
    }
}
